<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="hardcode2013.*" %>

<html>
	<head>
		<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
	</head>
	
	<body>
  
	<%@include file="include_header.jsp" %>
	
	<%
	final String currentUrl = "/activitylog.jsp";
	
	if (profile != null && profile.getIsAdmin()) {
		String pageStr = request.getParameter("page");
		int currentPage = 1;
		if (pageStr != null) {
			try {
				currentPage = Integer.parseInt(pageStr);
			} catch (Exception e) {
			}
		}
				
		if (currentPage <= 0) {response.sendRedirect(currentUrl); return;}
		AdminServ.LogQueryResult logRes = AdminServ.getEventLogs(currentPage);
		int totalPages = logRes.totalLogs / AdminServ.resultsPerPage + (logRes.totalLogs % AdminServ.resultsPerPage == 0 ? 0 : 1);
		if (currentPage > totalPages && logRes.totalLogs > 0) {response.sendRedirect(currentUrl + "?page=" + totalPages); return;}
		
		String pageLbl = "Total Items: " + logRes.totalLogs;
		if (logRes.totalLogs > 0)
			pageLbl += ", " +
			"Showing: " + ((currentPage - 1) * AdminServ.resultsPerPage + 1) + "-" + (currentPage * AdminServ.resultsPerPage > logRes.totalLogs ? logRes.totalLogs : currentPage * AdminServ.resultsPerPage) + ", " +
			"Page: " + currentPage + " / " + totalPages +
			(currentPage > 1 ? " <a href=\"" + EncodeUtil.encodeHtml(currentUrl) + "?page=" + (currentPage - 1) + "\">Prev</a>" : "") +
			(currentPage < totalPages ? " <a href=\"" + EncodeUtil.encodeHtml(currentUrl) + "?page=" + (currentPage + 1) + "\">Next</a>" : "");
		%>
		
    	<fieldset>
			<legend>Activity Log</legend>
			<%= pageLbl %>
	    	<table border="1" width="100%">
		    <tr>
			    <td>Time</td>
		  	    <td>Username</td>
		   	    <td>Event</td>
		    </tr>
			<%
				for (LogProfile i : logRes.logList) {
			%>
			    <tr>
				    <td><%=EncodeUtil.encodeHtml(i.getDateTime())%></td>
				    <td><%=EncodeUtil.encodeHtml(i.getUsername())%></td>
			  	    <td><%=EncodeUtil.encodeHtml(i.getEvent()).replaceAll("\n", "<br>")%></td>
			    </tr>
				<%
			}
			%>
			</table>
		</fieldset>
		<%
	}
	%>
	</body>
</html>